﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using BookShop.DAL;
using BookShop.DTO;

namespace BookShop.BLL
{
    public class DiscountService
    {
        public static Discount Add(Discount discount)
        {
            using (var repository = new DiscountRepository())
            {
                return repository.Add(discount);
            }
        }

        public static Discount Update(Discount discount)
        {
            using (var repository = new DiscountRepository())
            {
                return repository.Update(discount);
            }
        }

        public static bool Delete(long discountId)
        {
            using (var repository = new DiscountRepository())
            {
                var discount = GetById(discountId);
                return discount != null && repository.Delete(discount);
            }
        }

        public static List<Discount> GetAll()
        {
            using (var repository = new DiscountRepository())
            {
                return repository.GetAll().ToList();
            }
        }

        public static List<Discount> GetAllEffectiveSale()
        {
            using (var repository = new DiscountRepository())
            {
                return repository.FindAll(d => d.EndDate >= DateTime.Now).ToList();
            }
        }

        public static Discount GetById(long discountId)
        {
            using (var repository = new DiscountRepository())
            {
                return repository.Find(d => d.Id == discountId);
            }
        }

        public static List<Discount> Search(long? discountId, long? bookId, string fromStartDate, string toStartDate,
            string fromEndDate, string toEndDate, int page, int pageSize, out int count)
        {
            var repository = new DiscountRepository();
            var returnList = new List<Discount>();
            if (discountId.HasValue)
            {
                var discount = GetById(discountId.Value);
                if (discount != null)
                {
                    returnList.Add(discount);
                }
            }
            else
            {
                returnList = repository.GetAll().ToList();
                if (bookId.HasValue)
                {
                    returnList = returnList.FindAll(d => d.BookId == bookId.Value);
                }
                DateTime newFromEndDate;
                var isValidFromEndDate = DateTime.TryParse(fromEndDate, CultureInfo.CreateSpecificCulture("vi-VN"),
                    DateTimeStyles.None, out newFromEndDate);
                DateTime newToEndDate;
                var isValidToEndDate = DateTime.TryParse(toEndDate, CultureInfo.CreateSpecificCulture("vi-VN"),
                    DateTimeStyles.None, out newToEndDate);
                if (isValidFromEndDate && isValidToEndDate)
                {
                    returnList =
                        returnList.FindAll(a => a.EndDate >= newFromEndDate && a.EndDate <= newToEndDate);
                }
                if (isValidFromEndDate && !isValidToEndDate)
                {
                    returnList =
                        returnList.FindAll(a => a.EndDate >= newFromEndDate);
                }
                if (!isValidFromEndDate && isValidToEndDate)
                {
                    returnList =
                        returnList.FindAll(a => a.EndDate <= newToEndDate);
                }
                DateTime newFromStartDate;
                var isValidFromStartDate = DateTime.TryParse(fromStartDate, CultureInfo.CreateSpecificCulture("vi-VN"),
                    DateTimeStyles.None, out newFromStartDate);
                DateTime newToStartDate;
                var isValidToStartDate = DateTime.TryParse(toStartDate, CultureInfo.CreateSpecificCulture("vi-VN"),
                    DateTimeStyles.None, out newToStartDate);
                if (isValidFromStartDate && isValidToStartDate)
                {
                    returnList =
                        returnList.FindAll(a => a.StartDate >= newFromStartDate && a.StartDate <= newToStartDate);
                }
                if (isValidFromStartDate && !isValidToStartDate)
                {
                    returnList =
                        returnList.FindAll(a => a.StartDate >= newFromStartDate);
                }
                if (!isValidFromStartDate && isValidToStartDate)
                {
                    returnList =
                        returnList.FindAll(a => a.StartDate <= newToStartDate);
                }
            }
            count = returnList.Count;
            returnList = returnList.Skip((page - 1) * pageSize).Take(pageSize).ToList();
            foreach (var discount in returnList)
            {
                discount.BookTitle = (BookService.GetById(discount.BookId) ?? new Book()).Title;
            }
            return returnList;
        }
    }
}
